package com.primaryedu.adapter;

import java.util.ArrayList;
import java.util.List;

import com.bumptech.glide.Glide;
import com.primaryedu.R;
import com.primaryedu.bean.BannerBean;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

@SuppressLint("CutPasteId")
public class BannerAdapter extends BaseAdapter {
    private Context context;
    LayoutInflater inflater;
//    LoadImage loadImage;
    private List<BannerBean> bannerLists = new ArrayList<>();

    private Integer[] imgs = new Integer[]{
            R.drawable.icon_custom_binner,
            R.drawable.icon_health_binner,
            R.drawable.icon_custom_binner,
            R.drawable.icon_health_binner,
    };

    public BannerAdapter(Context context, List<BannerBean> bannerLists) {
        this.context = context;
        this.bannerLists = bannerLists;
//        loadImage = LoadImage.getInstance();
//		inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE) ;
        if (!(context instanceof Activity)) {
            new Intent().addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.item_banner, null);
            viewHolder = new ViewHolder();
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image);
        viewHolder.layout = (LinearLayout) convertView.findViewById(R.id.layout);

        int temp = position + 1;
//		viewHolder.imageView.setBackgroundResource(imgs[temp%imgs.length]);

        BannerBean bean = bannerLists.get(position % bannerLists.size());
        Glide.with(context)
                .load(bean.getPicUrl())
                .placeholder(R.drawable.default_error)
                .error(R.drawable.default_error)
                .centerCrop()
                .crossFade()
                .into(viewHolder.imageView);
//		viewHolder.imageView.setTag(bean.getPicUrl());
//		loadImage.addTask(bean.getPicUrl(), viewHolder.imageView);
//		loadImage.doTask();

        LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.layout);

        for (int i = 0; i < bannerLists.size(); i++) {

            ImageView roundView = new ImageView(context);

            if (i == temp % bannerLists.size()) {
                roundView.setBackgroundResource(R.drawable.round_default);
            } else {
                roundView.setBackgroundResource(R.drawable.round_white);
            }

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.setMargins(6, 6, 6, 6);    //left,top,right, bottom
            roundView.setLayoutParams(params);
            layout.addView(roundView);
        }

        return convertView;
    }

    class ViewHolder {
        ImageView imageView;
        LinearLayout layout;
    }
}
